import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

enum MyCounterEvent2 { increment, decrement }

class MyCounterBloc2 extends Bloc<MyCounterEvent2, int> {
  MyCounterBloc2() : super(0);

  @override
  Stream<int> mapEventToState(MyCounterEvent2 event) async* {
    switch (event) {
      case MyCounterEvent2.increment:
        yield state + 1;
        break;
      case MyCounterEvent2.decrement:
        yield state - 1;
        break;
      default:
        addError(Exception("unsupported event"));
        break;
    }
  }
}

/// 原来版本:2.1.1
// enum ThemeEvent2 { toogle }
//
// class ThemeBloc2 extends Bloc<ThemeEvent2, ThemeData> {
//   ThemeBloc2(ThemeData initialState) : super(initialState);
//
//   @override
//   // TODO: implement initialState
//   ThemeData get initialState => ThemeData.light();
//
//   @override
//   Stream<ThemeData> mapEventToState(ThemeEvent2 event) async* {
//     switch (event) {
//       case ThemeEvent2.toogle:
//         yield state == ThemeData.dark() ? ThemeData.light() : ThemeData.dark();
//         break;
//     }
//   }
// }

/// 6.1.3 版本
class ThemeCubit extends Cubit<ThemeData> {
  static final _lightTheme = ThemeData(
      floatingActionButtonTheme: const FloatingActionButtonThemeData(foregroundColor: Colors.white),
      brightness: Brightness.light);
  static final _darkTheme = ThemeData(
      floatingActionButtonTheme: const FloatingActionButtonThemeData(foregroundColor: Colors.black),
      brightness: Brightness.dark);

  ThemeCubit() : super(_lightTheme);

  void toogleTheme() {
    emit(state.brightness == Brightness.dark ? _lightTheme : _darkTheme);
  }
}
